﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WitsWebApi.DataContext.Repositories;
using WitsWebApi.Entity.Entities;
using WitsWebApi.DataContext.Extensions;
using WitsWebApi.Entity.EntityParam;
using WitsWebApi.Entity.PageHelper;
using WitsWebApi.DataContext.RepositoryBase;

namespace WitsWebApi.Application.Services
{
    public class AdminDetailService : IAdminDetailService
    {
        private readonly IAdminRepository _adminRepository;
        private readonly IMapper _mapper;

        public AdminDetailService(IMapper mapper, IAdminRepository adminRepository
            )
        {
            if (mapper == null || adminRepository == null)
            {
                throw new ArgumentNullException();
            }
            _mapper = mapper;
            _adminRepository = adminRepository;
        }

        /// <summary>
        /// 根据用户名获取指定用户
        /// </summary>
        /// <param name="AdminName"></param>
        /// <returns></returns>
        public async Task<Admin> GetAdminDetailByName(string AdminName)
        {
            try
            {
                Expression<Func<Admin, bool>> filter = x => true;
                if (!string.IsNullOrWhiteSpace(AdminName))
                    filter = filter.And(a => a.AdminUserName == AdminName);

                var result = await _adminRepository.Filter(filter).FirstOrDefaultAsync();

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="adminParam"></param>
        /// <returns></returns>
        public async Task<bool> AddAdminDetail(AdminParam adminParam)
        {
            try
            {
                var adminResult = _mapper.Map<Admin>(adminParam);
                await _adminRepository.AddAsync(adminResult);
                var result = await _adminRepository.SaveAsync();
                if (result > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 通过ID 删除指定用户
        /// </summary>
        /// <param name="AdminID"></param>
        /// <returns></returns>
        public async Task<bool> DelAdminByAdminID(int[] AdminIDs)
        {
            try
            {
                Expression<Func<Admin, bool>> filter = x => AdminIDs.Contains(x.AdminID);
                var result = _adminRepository.Filter(filter).ToList();
                if (result != null && result.Count() > 0)
                {
                    //执行删除操作
                    await _adminRepository.DeleteAsync(filter);
                    int status = await _adminRepository.SaveAsync();
                    if (status > 0)
                        return true;
                    else
                        return false;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改指定用户信息
        /// </summary>
        /// <param name="adminParam"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAdminDetail(Admin adminParam)
        {
            try
            {
                if (adminParam == null)
                    return false;

                Expression<Func<Admin, bool>> filter = x => x.AdminID == adminParam.AdminID;
                var result = _adminRepository.Exists(filter);
                if (!result)
                    return false;

                _adminRepository.Update(adminParam);
                var status = await _adminRepository.SaveAsync();
                if (status > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="adminPageResult"></param>
        /// <returns></returns>
        public async Task<Paged<AdminPage>> AdminPageData(AdminPageResult adminPageResult)
        {
            try
            {
                Paged<AdminPage> list_admin = new Paged<AdminPage>();
                int total = 0;
                Expression<Func<Admin, bool>> filter = x => true;

                if (!string.IsNullOrWhiteSpace(adminPageResult.AdminName))
                    filter = filter.And(x => x.AdminName.Contains(adminPageResult.AdminName));

                Expression<Func<Admin, dynamic>> orderBy = x => x.AdminID;

                var result = _adminRepository.Filter(filter, orderBy, SortOrder.Ascending, out total, adminPageResult.PageIndex, adminPageResult.PageSize);
                if (result != null && result.Count() > 0)
                {
                    var name = _mapper.Map<List<AdminPage>>(result);
                    foreach (var item in name)
                    {
                        item.adminPowerName = item.AdminPower == 0 ? "测试" : "总监";
                    }

                    var list = new PageDataContext<AdminPage>();
                    list_admin = list.GetPagedData(adminPageResult, total, result.Count(), name);
                    return list_admin;
                }

                return null;
            }
            catch (Exception ex)
            {

                throw;
            }
        }



    }
}
